package com.jackrain.nea.oms.services;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.oms.dao.DatavDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

/**
 * @Discription: Created with IntelliJ IDEA.
 * @Author xyc
 * @Date 创建时间：2018/10/23 15:13
 */
@Component
@Slf4j
public class DatavServies {

    @Autowired
    private DatavDao datavDao;

    private static final String ORDERSHOPSTATISTIC = "T_V_ORDERSHOPSTATISTIC";
    private static final String PRODUCTNUMSTATIC = "T_V_PRODUCTNUMSTATIC";
    private static final String PRODUCTPRICESTATIC = "T_V_PRODUCTPRICESTATIC";
    private static final String MEMBERPRICESTATIC = "T_V_MEMBERPRICESTATIC";
    private static final String REGIONORDERSTATIC = "T_V_REGIONORDERSTATIC";
    private static final String RETAILSTORE = "V_RETAIL_STORE";
    private static final String RETAILQH = "O2O_RETAIL_QH";

    /**
     * 同步数据到rds
     * @param jsonObject
     * @param msgKey
     * @param topic
     * @param tag
     * @throws Exception
     */
    public void synchronizeData(JSONObject jsonObject, String msgKey, String topic, String tag) throws Exception {
        try {
            String type = jsonObject.getString("type");
            if(type == null || "".equals(type)){
                log.error("----> type非法参数。msgKey:"+ msgKey + " topic:"+ topic + " tag:" + tag);
                return;
            }

            type = type.toUpperCase();

            switch(type) {
                //店铺单量统计
                case ORDERSHOPSTATISTIC : {
                    setOrdershopstatistic(jsonObject, msgKey, topic, tag);
                    break;
                }
                //款号成交数量信息统计
                case PRODUCTNUMSTATIC : {
                    setProductnumstatic(jsonObject, msgKey, topic, tag);
                    break;
                }
                //款号成交金额信息统计
                case PRODUCTPRICESTATIC : {
                    setProductPriceStatic(jsonObject, msgKey, topic, tag);
                    break;
                }
                //会员成交金额统计
                case MEMBERPRICESTATIC : {
                    setMemberPriceSTstic(jsonObject, msgKey, topic, tag);
                    break;
                }
                //区域热力图，新增区域订单销售数量
                case REGIONORDERSTATIC : {
                    setRegionOrderStatic(jsonObject, msgKey, topic, tag);
                    break;
                }

                //线下单量数据
                case RETAILSTORE : {
                    setRetailStore(jsonObject, msgKey, topic, tag);
                    break;
                }

                //云仓单量数据
                case RETAILQH : {
                    setRetailQh(jsonObject, msgKey, topic, tag);
                    break;
                }

                default:{
                    break;
                }

            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 店铺单量统计
     * @param jsonObject
     */
    @Transactional(rollbackFor = Exception.class)
    public void setOrdershopstatistic(JSONObject jsonObject, String msgKey, String topic, String tag){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        try{
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            if(jsonArray.isEmpty()){
                log.debug("店铺单量为空 msgKey:"+ msgKey + " topic:"+ topic + " tag:" + tag);
                return;
            }
            for (Object object: jsonArray){
                JSONObject objectdetail = JSONObject.parseObject(object.toString());
                datavDao.recOrderShopStatic(objectdetail, time);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 款号成交数量信息统计
     * @param jsonObject
     */
    @Transactional(rollbackFor = Exception.class)
    public void setProductnumstatic(JSONObject jsonObject, String msgKey, String topic, String tag){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        try{
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            if(jsonArray.isEmpty()){
                log.debug("款号成交数量信息为空 msgKey:"+ msgKey + " topic:"+ topic + " tag:" + tag);
                return;
            }
            for (Object object: jsonArray){
                JSONObject objectdetail = JSONObject.parseObject(object.toString());
                datavDao.recProductNumStatic(objectdetail, time);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 款号成交金额信息统计
     * @param jsonObject
     */
    @Transactional(rollbackFor = Exception.class)
    public void setProductPriceStatic(JSONObject jsonObject, String msgKey, String topic, String tag){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        try{
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            if(jsonArray.isEmpty()){
                log.debug("款号成交金额信息为空 msgKey:"+ msgKey + " topic:"+ topic + " tag:" + tag);
                return;
            }
            for (Object object: jsonArray){
                JSONObject objectdetail = JSONObject.parseObject(object.toString());
                datavDao.recProductPriceStatic(objectdetail, time);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 会员成交金额统计
     * @param jsonObject
     */
    @Transactional(rollbackFor = Exception.class)
    public void setMemberPriceSTstic(JSONObject jsonObject, String msgKey, String topic, String tag){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        try{
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            if(jsonArray.isEmpty()){
                log.debug("会员成交金额信息为空 msgKey:"+ msgKey + " topic:"+ topic + " tag:" + tag);
                return;
            }
            for (Object object: jsonArray){
                JSONObject objectdetail = JSONObject.parseObject(object.toString());
                datavDao.recMemberPriceStatic(objectdetail, time);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 区域热力图，新增区域订单销售数量
     * @param jsonObject
     */
    @Transactional(rollbackFor = Exception.class)
    public void setRegionOrderStatic(JSONObject jsonObject, String msgKey, String topic, String tag){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        try{
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            if(jsonArray.isEmpty()){
                log.debug("区域热力图，新增区域订单销售数量信息为空 msgKey:"+ msgKey + " topic:"+ topic + " tag:" + tag);
                return;
            }
            for (Object object: jsonArray){
                JSONObject objectdetail = JSONObject.parseObject(object.toString());
                datavDao.recRegionOrderStatic(objectdetail, time);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 线下单量数据
     * @param jsonObject
     */
    @Transactional(rollbackFor = Exception.class)
    public void setRetailStore(JSONObject jsonObject, String msgKey, String topic, String tag){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        try{
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            if(jsonArray.isEmpty()){
                log.debug("线下单量数据信息为空 msgKey:"+ msgKey + " topic:"+ topic + " tag:" + tag);
                return;
            }
            for (Object object: jsonArray){
                JSONObject objectdetail = JSONObject.parseObject(object.toString());
                datavDao.recRetailStore(objectdetail, time);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 云仓单量数据
     * @param jsonObject
     */
    @Transactional(rollbackFor = Exception.class)
    public void setRetailQh(JSONObject jsonObject, String msgKey, String topic, String tag){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        try{
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            if(jsonArray.isEmpty()){
                log.debug("云仓单量数据信息为空 msgKey:"+ msgKey + " topic:"+ topic + " tag:" + tag);
                return;
            }
            for (Object object: jsonArray){
                JSONObject objectdetail = JSONObject.parseObject(object.toString());
                datavDao.recRetailQh(objectdetail, time);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

